package com.wxzz.elearing.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.comment.dto.SkillVideoLogDto;
import com.wxzz.elearing.comment.entity.Skill;
import com.wxzz.elearing.comment.entity.SkillVideoLog;
import com.wxzz.elearing.comment.mapper.SkillVideoLogMapper;
import com.wxzz.elearing.comment.service.ISkillVideoLogService;
import com.wxzz.elearing.comment.service.openfeign.IWxUserApiFeign;
import com.wxzz.elearing.comment.vo.SkillVideoLogVo;
import com.wxzz.elearing.comment.vo.SkillVo;
import com.wxzz.elearning.common.dto.FeignWxUserDto;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.seata.common.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 技能沉淀视频观看记录 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-05-17
 */
@Service
public class SkillVideoLogServiceImpl extends ServiceImpl<SkillVideoLogMapper, SkillVideoLog> implements ISkillVideoLogService {

    @Autowired
    SkillVideoLogMapper skillVideoLogMapper;

    @Autowired
    IWxUserApiFeign wxUserApiFeign;

    @Override
    public ReturnVO<JgPageVo<List<SkillVideoLogVo>>> listSkillWatchLog(SkillVideoLogDto skillVideoLogDto, Long skillId) {
        PageHelper.startPage(skillVideoLogDto.getCurPage(), skillVideoLogDto.getMaxPage());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("skill_id", skillId);
        queryWrapper.orderByDesc("create_time");
        List<SkillVideoLog> skillVideoLogs = skillVideoLogMapper.selectList(queryWrapper);
        List<SkillVideoLogVo> skillVideoLogVos = new ArrayList<>();

        // foreach
        for (SkillVideoLog skillVideoLog: skillVideoLogs) {
            // user feign
            SkillVideoLogVo vo = new SkillVideoLogVo();
            BeanUtils.copyProperties(skillVideoLog, vo);
            // 根据用户查询 查询发布者信息
            FeignWxUserDto userDto = wxUserApiFeign.selectFeignWxUserDtoByWxUserId(skillVideoLog.getWatchUserId());
            vo.setUserInfo(userDto);
            if (userDto != null) {
                vo.setIcon(userDto.getIcon());
                vo.setNickName(userDto.getNickname());
                vo.setDepartment(userDto.getOrganizationName());
            } else {
                vo.setIcon("");
                vo.setNickName("未知");
                vo.setDepartment("未知");
            }
            skillVideoLogVos.add(vo);
        }

        return new ReturnVO("查询成功", new JgPageVo<>(new PageInfo(skillVideoLogVos)));
    }
}
